<?php
class ModelCatalogInformation extends Model {
	public function getSortBirthday()
	{
		$sql = "SELECT max(sort_order) AS minfo FROM " . DB_PREFIX . "information";
		$query = $this->db->query($sql);
		return $query->row['minfo'];
	}
	public function addBirthday($data) {
		
		$this->db->query("INSERT INTO " . DB_PREFIX . "information SET module = '" . $data['module'] . "',sort_order = '" . ((int) $this->getSortBirthday()+1) . "', status = '" . (int)$data['status'] . "'");

		$information_id = $this->db->getLastId(); 
		
		if (isset($data['nick_chat'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "information SET nick_chat = '" . $this->db->escape($data['nick_chat']) . "' WHERE information_id = '" . (int)$information_id . "'");
		}
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "information SET image = '" . $this->db->escape($data['image']) . "' WHERE information_id = '" . (int)$information_id . "'");
		}
		
		if (isset($data['link'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "information SET link = '" . $this->db->escape($data['link']) . "' WHERE information_id = '" . (int)$information_id . "'");
		}
		
		if (isset($data['namechild'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "information SET namechild = '" . $this->db->escape($data['namechild']) . "' WHERE information_id = '" . (int)$information_id . "'");
		}
		if (isset($data['sex'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "information SET sex = '" . $this->db->escape($data['sex']) . "' WHERE information_id = '" . (int)$information_id . "'");
		}
		if (isset($data['birthday'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "information SET birthday = '" . $this->db->escape($data['birthday']) . "' WHERE information_id = '" . (int)$information_id . "'");
		}	
		
		foreach ($data['information_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "',short = '" . $this->db->escape($value['short']) . "'");
		}
		
		if (isset($data['information_store'])) {
			foreach ($data['information_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "information_to_store SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "'");
			}
		}

		if (isset($data['information_layout'])) {
			foreach ($data['information_layout'] as $store_id => $layout) {
				if ($layout) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "information_to_layout SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
				}
			}
		}
				
		if ($data['keyword']) {
			$query = $this->db->query("SELECT count(*) as keyword FROM " . DB_PREFIX . "url_alias WHERE `keyword` =  '".$this->db->escape($data['keyword'])."'");
			$keyword = $query->row['keyword'];
			if($keyword > 0) {
				$data['keyword'] = $data['keyword'].' '. $information_id;
			}
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'information_id=" . (int)$information_id . "', keyword = '" . $this->db->escape($data['keyword']) . "',module = '" . $this->request->post['module'] . "'");
		}
		
		$this->cache->delete('information');
		return true;
	}
	
	public function getInformation($information_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) LEFT JOIN " . DB_PREFIX . "information_to_store i2s ON (i.information_id = i2s.information_id) WHERE i.information_id = '" . (int)$information_id . "' AND id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND i2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND i.status = '1'");
	
		return $query->row;
	}
	public function getBirthdayBabys() {
		//$date1 = date('Y-m-d H:i:s');
		//$date2 = date('Y-m-d H:i:s');
		//AND date(i.birthday) >= date(now()) AND date(i.birthday) <= date(DATE_ADD(now(),INTERVAL 7 DAY))	
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) LEFT JOIN " . DB_PREFIX . "information_to_store i2s ON (i.information_id = i2s.information_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND i.status = '1' AND i.sort_order <> '-1'  AND module='birthday'  ORDER BY i.birthday DESC");
		
		return $query->rows;
	}
	public function getInformations() {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) LEFT JOIN " . DB_PREFIX . "information_to_store i2s ON (i.information_id = i2s.information_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND i2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND i.status = '1' AND i.sort_order <> '-1' ORDER BY i.sort_order, LCASE(id.title) ASC");
		
		return $query->rows;
	}
	
	public function getInformations2($module) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) LEFT JOIN " . DB_PREFIX . "information_to_store i2s ON (i.information_id = i2s.information_id) WHERE i.module='".$module."' AND id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND i2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND i.status = '1' AND i.sort_order <> '-1' ORDER BY i.sort_order DESC");
		
		return $query->rows;
	}
	
	public function getInformationLayoutId($information_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");
		 
		if ($query->num_rows) {
			return $query->row['layout_id'];
		} else {
			return $this->config->get('config_layout_information');
		}
	}	
	public function getInformations3($data = array()) {		
			$sql = "SELECT * FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) WHERE i.module='".$data['module']."' AND id.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
			if (isset($data['frontpage'])) {			
					$sql .= ' AND frontpage=1';
			}	
			if (isset($data['search'])) {			
					$sql .= " AND title like '%".$data['search']."%'";
			}	
			
			if (isset($data['order'])) {			
					$sql .= " ORDER BY i.sort_order " . $data['order'];	
			} else {
				$sql .= " ORDER BY i.sort_order DESC";	
			}	
			
			
			if (isset($data['start']) && isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}		

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
			return $query->rows;
	}
	public function getTotalInformations3($data = array()) {
		//$sql  = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information where status=1 ";
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) WHERE i.module='".$data['module']."' AND id.language_id = '" . (int)$this->config->get('config_language_id') . "'";
      	
		if (isset($data['module'])) {			
				$sql .= " AND module='".$data['module']."'";
		}	
		if (isset($data['frontpage'])) {			
				$sql .= ' AND frontpage=1';
		}	
		if (isset($data['search'])) {			
					$sql .= " AND title like '%".$data['search']."%'";
		}	
			
		$query = $this->db->query($sql);
		return $query->row['total'];
	}	
	
	
}
?>